const { SuccessSend } = require("../utils/response");
const NoticeModel = require("../models/notice");

class NoticeController {
    static async create(req, res, next) {
        try {
            const notice = new NoticeModel({
                title: req.body.title,
                content: req.body.content,
            });
            await notice.save();
            return SuccessSend(res, {
                success: true,
                message: "创建成功",
                data: {
                    _id: notice._id,
                    title: notice.title,
                },
            });
        } catch (error) {
            next(error);
        }
    }

    static async getAll(req, res, next) {
        try {
            const notices = await NoticeModel.find({}).select(
                "+status +createAt"
            );
            return SuccessSend(res, {
                success: true,
                message: "获取成功",
                data: notices,
            });
        } catch (error) {
            next(error);
        }
    }

    static async list(req, res, next) {
        try {
            const notices = await NoticeModel.find({ status: 0 });
            return SuccessSend(res, {
                success: true,
                message: "获取成功",
                data: notices,
            });
        } catch (error) {
            next(error);
        }
    }
}

module.exports = NoticeController;
